package com.superme.service.impl;

import com.superme.entity.PersonEntity;
import com.superme.repository.PersonRepo;
import com.superme.repository.PersonSpecification;
import com.superme.repository.PersonV2Specification;
import com.superme.service.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Administrator on 2018/1/19.
 * 贫血模型中的 Service层
 */
@Service
public class ExampleServiceImpl implements ExampleService {
    /**
     * 或者直接采用
     *
     * @Autowired
     * private final PersonRepo pr;
     */

    private final PersonRepo pr;

    @Autowired
    public ExampleServiceImpl(PersonRepo _pr) {
        this.pr = _pr;
    }

    /*
          根据查询条件查询相关数据
         */
    public Page<PersonEntity> findallByCondition(String name, String nickName, int pageNum, int pageSize, Sort.Direction sd, String orderProprtties) {

        //Pageable是接口，PageRequest是接口实现
        //PageRequest的对象构造函数有多个，page是页数，初始值是0，size是查询结果的条数，后两个参数参考Sort对象的构造方法
        Pageable pageable = new PageRequest(pageNum, pageSize, sd, orderProprtties);
        //设置查询条件类
        PersonSpecification<PersonEntity> personSpecification = new PersonSpecification();
        personSpecification.setName(name);
        personSpecification.setNickname(nickName);
        //进行查询
        return pr.findAll(personSpecification, pageable);
    }

    @Override
    public Page<PersonEntity> findallBySearchText(String searchText, int pageNum, int pageSize, Sort.Direction sd, String sortName) {
        Pageable pageable = new PageRequest(pageNum, pageSize);
        if(sortName !=null && !sortName.equals("")) {
             pageable = new PageRequest(pageNum, pageSize, sd, sortName);
        }
        Page<PersonEntity> Result = null;
        if(searchText == null || searchText=="")
        {
            Result =  pr.findAll(pageable);
        }
        else {
            PersonV2Specification<PersonEntity> personSpecification = new PersonV2Specification(searchText);
            Result =  pr.findAll(personSpecification, pageable);
        }
        return Result;
    }

    /*
    根据ID获取对象
     */
    public PersonEntity findByID(int id) {
        return pr.findById(id);
    }

    /*
       新增相关信息
        */
    @Transactional
    public PersonEntity addPerson(String name, String nickName, Integer age, String date, Integer height) {
        PersonEntity pe = new PersonEntity();
        pe.setName(name);
        pe.setNickname(nickName);
        pe.setAge(age);
        if (date != null) {
            Timestamp ts = Timestamp.valueOf(date + " 00:00:00");
            pe.setBirthday(ts);
        }
        pe.setHeight(height);
        return pr.save(pe);

    }

    @Transactional
    public PersonEntity addPerson(String name, String nickName, Integer age, Timestamp date, Integer height) {
        PersonEntity pe = new PersonEntity();
        pe.setName(name);
        pe.setNickname(nickName);
        pe.setAge(age);
        pe.setBirthday(date);
        pe.setHeight(height);
        return pr.save(pe);

    }

    /*
    更新保存相关信息
     */
    @Transactional
    public PersonEntity savePerson(int id, String name, String nickName, Integer age, String date, Integer height) {
        PersonEntity pe = this.pr.findById(id);
        if (pe != null) {
            pe.setName(name);
            pe.setNickname(nickName);
            pe.setAge(age);
            if (date != null) {
                Timestamp ts = Timestamp.valueOf(date + " 00:00:00");
                pe.setBirthday(ts);
            }
            pe.setHeight(height);
            return pr.save(pe);
        }
        return null;
    }


    @Transactional
    public PersonEntity savePerson(int id, String name, String nickName, Integer age, Timestamp date, Integer height) {
        PersonEntity pe = this.pr.findById(id);
        if (pe != null) {
            pe.setName(name);
            pe.setNickname(nickName);
            pe.setAge(age);
            pe.setBirthday(date);
            pe.setHeight(height);
           return pr.save(pe);
        }
        return null;
    }

    /*
       删除相关信息
         */
    @Transactional
    public boolean deletePerson(int id) {
        PersonEntity pe = this.pr.findById(id);
        if (pe != null) {

            //pr.deleteById(id);
            pr.deletePersonById(id);
        }
        return true;
    }

}
